package com.tako;

import javafx.beans.property.ObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Callback;

import java.util.List;

public class Controller {

    private Service service = new Service();

    @FXML
    private VBox box;

    // File
    @FXML
    private MenuItem menuQuit;

    // Edit
    @FXML
    private Button clearBtn;

    @FXML
    private Button addBtn;

    @FXML
    private Button addFolderBtn;

    @FXML
    private Button startBtn;

    @FXML
    private TableView<MergeTask> table;

    @FXML
    private TableColumn<MergeTask, Boolean> check;

    @FXML
    private TableColumn<MergeTask, String> src;

    @FXML
    private TableColumn<MergeTask, String> input;

    @FXML
    private TableColumn<MergeTask, Integer> size;

    @FXML
    private TableColumn<MergeTask, String> output;

    @FXML
    private TableColumn<MergeTask, String> status;

    @FXML
    void onStart(ActionEvent event) {
        ObservableList<MergeTask> tableItems = table.getItems();
        service.mergeFiles(tableItems);
    }

    @FXML
    void onAdd(ActionEvent event) {
        ObservableList<MergeTask> tasks = table.getItems();
        List<MergeTask> items = service.addFolder();
        if (!items.isEmpty()) {
            tasks.addAll(items);
        }
    }

    @FXML
    void onAddFolder(ActionEvent event) {
        ObservableList<MergeTask> tableItems = table.getItems();
        List<MergeTask> items = service.batchAddFolder();
        if (!items.isEmpty()) {
            tableItems.addAll(items);
        }
    }

    @FXML
    void onClear(ActionEvent event) {
        table.getItems().clear();
    }

    @FXML
    void onQuit(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("确认退出");
        alert.showAndWait().ifPresent(buttonType -> {
            if (buttonType == ButtonType.OK) {
                Window window = box.getScene().getWindow();
                ((Stage) window).close();
            }
        });
    }

    public void initialize() {
        check.setCellValueFactory(new PropertyValueFactory<>("check"));
        check.setCellFactory(CheckBoxTableCell.forTableColumn(check));
        src.setCellValueFactory(new PropertyValueFactory<>("src"));
        input.setCellValueFactory(new PropertyValueFactory<>("input"));
        size.setCellValueFactory(new PropertyValueFactory<>("size"));
        output.setCellValueFactory(new PropertyValueFactory<>("output"));
        status.setCellValueFactory(new PropertyValueFactory<>("status"));

        ObservableList<MergeTask> listItemMergePDF = FXCollections.observableArrayList();
        table.setItems(listItemMergePDF);

        box.addEventFilter(KeyEvent.KEY_RELEASED, this::boxKeyReleasedEvent);
    }


    private final KeyCombination ctrlS = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
    private final KeyCombination ctrlC = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN);
    private final KeyCombination ctrlA = new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN);
    private final KeyCombination ctrlF = new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN);
    private final KeyCombination ctrlQ = new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN);

    private void boxKeyReleasedEvent(KeyEvent e) {
        try {
            if (ctrlS.match(e)) {
                onStart(null);
            } else if (ctrlC.match(e)) {
                onClear(null);
            } else if (ctrlA.match(e)) {
                onAdd(null);
            } else if (ctrlF.match(e)) {
                onAddFolder(null);
            } else if (ctrlQ.match(e)) {
                onQuit(null);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
